package com.lemner.motion.activity;

import android.content.Intent;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.lemner.motion.R;
import com.lemner.motion.base.BaseActivity;
import com.lemner.motion.base.BaseListener;
import com.lemner.motion.base.BaseModel;
import com.lemner.motion.bean.LocationBean;
import com.lemner.motion.util.FullScreen;

import java.io.Serializable;
import java.util.List;

import at.markushi.ui.CircleButton;

public class FindWayActivity extends BaseActivity implements AMap.InfoWindowAdapter {
    private  CircleButton btn_go;
    private MapView mMapView = null;
    private AMap aMap = null;
    private double latitude, longitude;
    private String schoolname,studentname;
    private  View infoWindow = null;
    private  List<String> list;

    @Override
    public int getLayoutFile() {
        return R.layout.activity_find_way;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         mMapView.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {
        FullScreen.fullScreen(this,0x33131313);
        mMapView = (MapView) findViewById(R.id.map);
        btn_go=(CircleButton)findViewById(R.id.btn_go);
        initLocation();
    }

    @Override
    public void initEvent() {
        btn_go.setOnClickListener(this);
    }


    @Override
    public void initData() {
        schoolname=getIntent().getStringExtra("shcoolname");
        studentname=getIntent().getStringExtra("studentname");
    }

    @Override
    public void onClick(View v) {
          if(v.getId()==R.id.btn_go){
              if(null!=list) {
              Intent intent=new Intent(FindWayActivity.this,MainActivity.class);
                  intent.putExtra("waylist", (Serializable) list);
                  startActivity(intent);
              }
          }
    }

   public void initLocation(){
        if (aMap == null) {
            aMap = mMapView.getMap();
            aMap.setInfoWindowAdapter(this);
        }
        MyLocationStyle myLocationStyle;
        myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类
       myLocationStyle.strokeColor(Color.argb(0, 0, 0, 0));// 设置圆形的边框颜色
       myLocationStyle.radiusFillColor(Color.argb(0, 0, 0, 0));// 设置圆形的填充颜色
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        aMap.setOnMyLocationChangeListener(new AMap.OnMyLocationChangeListener() {
           @Override
           public void onMyLocationChange(Location location) {
              latitude= location.getLatitude();
              longitude=location.getLongitude();
              String loca=latitude+","+longitude;
              upload(loca);
           }
       });
       // aMap.getUiSettings().setMyLocationButtonEnabled(true);设置默认定位按钮是否显示，非必需设置。
       aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
       aMap.moveCamera(CameraUpdateFactory.zoomTo(18));
       UiSettings mUiSettings;//定义一个UiSettings对象
       mUiSettings = aMap.getUiSettings();//实例化UiSettings类对象
       mUiSettings.setScaleControlsEnabled(true);//开启比例尺
    }
    BaseListener<LocationBean>listener=new BaseListener<LocationBean>() {
        @Override
        public void onResponse(LocationBean locationBean) {
            if(locationBean.getErrorCode().equals("0000"))
                forceonline(FindWayActivity.this);
            else  if(locationBean.getReturnCode().equals("0001")) {
                String name = locationBean.getData().getRouteName();
                 list = locationBean.getData().getThreePoint();
                LatLng latLng = new LatLng(latitude, longitude);
                Marker marker = aMap.addMarker(new MarkerOptions().position(latLng).title(studentname).snippet("您当前的位置距离" + name + "路线较近，\n建议你前往起点开始跑步").alpha(0));
                getInfoContents(marker);
                marker.showInfoWindow();
            }
            else
                showToast("发生异常错误");
        }
        @Override
        public void onFail(String msg) {
         Log.i("1",msg);
        }
    };


    public void upload(String loca){
        BaseModel<LocationBean> model=new BaseModel<>();
        retrofit2.Call<LocationBean>call=  model.service.uploadLocation(LoginActivity.studentid,schoolname,loca,LoginActivity.session);
        model.callEnqueue(call,listener);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        if(infoWindow == null) {
            infoWindow = LayoutInflater.from(this).inflate(
                    R.layout.custom_info_window, null);
        }
        render(marker, infoWindow);
        return infoWindow;
//加载custom_info_window.xml布局文件作为InfoWindow的样式
//该布局可在官方Demo布局文件夹下找到
    }

    public void render(Marker marker, View view) {
//如果想修改自定义Infow中内容，请通过view找到它并修改
        String title = marker.getTitle();
        TextView titleUi = ((TextView) view.findViewById(R.id.mark_title));
        if (title != null) {
            SpannableString titleText = new SpannableString(title);
            titleText.setSpan(new ForegroundColorSpan(Color.RED), 0, titleText.length(), 0);
            titleUi.setTextSize(10);
            titleUi.setText(titleText);

        } else {
            titleUi.setText("");
        }
        String snippet = marker.getSnippet();
        TextView snippetUi = ((TextView) view.findViewById(R.id.mark_snippet));
        if (snippet != null) {
            SpannableString snippetText = new SpannableString(snippet);
            if(snippet.length()==28)
                snippetText.setSpan(new ForegroundColorSpan(Color.argb(255,153,204,0)), 8, 11, 0);
            else
                snippetText.setSpan(new ForegroundColorSpan(Color.argb(255,153,204,0)), 8, 12, 0);
            snippetUi.setTextSize(12);
            snippetUi.setText(snippetText);
        } else{
            snippetUi.setText("");
        }

    }


}
